package GUI;


public class HorizontalScrollBar extends Component
{
    public float size;
    public float val;

    ////////////////////////////////////////////////////////////////////////////////////////////////////
    public HorizontalScrollBar()
    {

        selection = new Component();
        selection.backColor = Utils.globalProcessing.color(200, 200, 255, 100);
    }

    public void draw()
    {
        int bh = (int)(size * width);
        if(bh < 10) bh = 10;
       
        
        Utils.globalProcessing.stroke(80);
        Utils.globalProcessing.line(x , y+ height / 2, x+width , y + height / 2);
        
        // Draw selection region
        selection.x = x + (int)(val * width);
        selection.y = y;
        selection.height = height;
        selection.width = bh;
        selection.draw();
        Utils.gu.drawVGradient(selection.x, selection.y+ selection.height, selection.width, -selection.height, Utils.globalProcessing.color(200, 200, 200), 255, Utils.globalProcessing.color(255, 255, 255), 255, (float)0.5);

        // Handle scrollbar dragging.
        if(dragging)
        {
            float d = (float)(Utils.globalProcessing.mouseX - dragX) / width;
            float tmpV = val + d;
            if(tmpV >= 0 && tmpV + size <= 1)
            {
                val = tmpV;
            }
            dragX = Utils.globalProcessing.mouseX;
        }
    }
  
    public void mousePressed()
    {
        int bh = (int)(size * width);
        if(bh < 10) bh = 10;
        int x1 = x + (int)(val * width);
        int x2 = x1 + bh;

        if(Utils.globalProcessing.mouseX > x1 && Utils.globalProcessing.mouseX < x2)
        {
            dragging = true;
            dragX = Utils.globalProcessing.mouseX;
            return;
        }
    }
  
    public void mouseReleased()
    {
        dragging = false;
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////
    Component selection;
    private boolean dragging;
    private int dragX;
}